libname mylib 'g:\Dropbox\Wall Street Bets (Private)\Data';


*This program contains the information needed to create Figure 2  of the paper;
*The program references daily_panel_rfs which   is constructed in  Daily Panel RFS (Intermediate File) code;



data figures;
set mylib.daily_panel_rfs;

NET_SA2_PRE = NET_SA2 *  (1-POST_GME);
NON_RESEARCH2_PRE =  NON_RESEARCH2 *  (1-POST_GME);


keep ticker date cum_month gme_amc_flag post_gme ret1 RET5
 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 
week1-week13 NET_SA2_PRE NON_RESEARCH2_PRE;
run;

*compute cumulative returns;

data figures2;
set figures;
week2_cum = sum(of week1-week2);
week3_cum = sum(of week1-week3);
week4_cum = sum(of week1-week4);
week5_cum = sum(of week1-week5);
week6_cum = sum(of week1-week6);
week7_cum = sum(of week1-week7);
week8_cum = sum(of week1-week8);
week9_cum = sum(of week1-week9);
week10_cum = sum(of week1-week10);
week11_cum = sum(of week1-week11);
week12_cum = sum(of week1-week12);

week5_12 =  sum( of week5-week12);
run;


proc sort data=figures2;
by date;
run;

proc standard data=figures2 out=figures_fe mean=0;
by date;
var   ret5 ret21
net_dd2  NET_DD2_POST NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST
DD_and_SA DD_and_SA_POST DD_and_Non_Research DD_and_Non_Research_post net_dd2_pre
 ln_size ln_bm  abn_ret mom5 mom6_26     
BM_MISSING SIZE_MISSING   news_sentiment lag_sent5 lag_sent6_26 week1-week13 NET_SA2_PRE NON_RESEARCH2_PRE week5_12
week2_cum week3_cum week4_cum week5_cum week6_cum week7_cum week8_cum week9_cum week10_cum week11_cum week12_cum;
*where gme_amc_flag =0;
run;



*code below reports week2 cumulative returns;
*repace week2_cum with weekX_CUM where X = 3 to 12 to obtain other cumulative returns;
*one week return can be computed be replacing week2_cum with ret5;



%let yourdata=figures_fe;

%let firmid=ticker ;
%let time=cum_month;

%let y=week2_cum ;
%let x=  net_dd2 NET_DD2_POST  NET_SA2 NET_SA2_POST NON_RESEARCH2 NON_RESEARCH2_POST ln_size ln_bm   mom5 mom6_26       news_sentiment lag_sent5 lag_sent6_26   BM_MISSING SIZE_MISSING;
run;




proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;
